#include "TelephoneInfo.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

struct TelephoneInfo
{
    char *name;
    char *phone;
};
// typedef struct TelephoneInfo Telinfo;

Telinfo *CreateTelinfo()
{
    Telinfo *s = (Telinfo *)malloc(sizeof(Telinfo));
    if (s == NULL)
    {
        printf("Createelinfo malloc error!\n");
        exit(-1);
    }
    char name[15];
    printf("请输入用户姓名：\n");
    scanf("%s", name);
    char phone[12];
    printf("请输入用户电话号码：\n");
    scanf("%s", phone);
    s->name = strdup(name); // 堆上的字符串
    s->phone = strdup(phone);
    return s;
}

void PrintTelinfo(void *p)
{
    Telinfo *s = (Telinfo *)p;
    printf("姓名：%s 电话号码：%s\n", s->name, s->phone);
}

void ClearTelinfo(void *p)
{
    Telinfo *s = (Telinfo *)p;
    free(s->name);
    free(s->phone);
    free(s);
}

bool IsEqual(void *p1, void *p2)
{
    Telinfo *s = (Telinfo *)p1;
    const char *value = (const char *)p2;
    return strcmp(s->name, value) == 0 || strcmp(s->phone, value) == 0;
}

int FindEqual(void *p1, void *p2)
{
    Telinfo *s = (Telinfo *)p1;
    const char *value = (const char *)p2;
    // return strcmp(s->name, value) || strcmp(s->phone, value);
    return strcmp(s->name, value);
}

bool FindEqual2(void *p1, void *p2)
{
    Telinfo *s1 = (Telinfo *)p1;
    Telinfo *s2 = (Telinfo *)p2;
    // const char *value = (const char *)p2;
    // return strcmp(s->name, value) || strcmp(s->phone, value);
    return strcmp(s1->name, s2->name) == 0;
}

int BiggerName(void *p1, void *p2)
{
    Telinfo *s1 = (Telinfo *)p1;
    Telinfo *s2 = (Telinfo *)p2;
    return strcmp(s1->name, s2->name);
}

bool FuzzyEqual(void *p1, void *p2)
{
    Telinfo *s1 = (Telinfo *)p1;
    const char *value = (const char *)p2;
    return strstr(s1->name, value) != NULL;
}
